<template>
  <div v-enter-helper="getList" class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleAdd">添加</el-button>
          <el-form
            ref="dataList2"
            :inline="true"
            :model="listQuery"
            class="demo-form-inline"
          >
            <el-form-item label="">
              <el-input
                v-model="listQuery.user_code"
                clearable
                placeholder="请输入用户编号"
                type="text"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item">
        <el-table
          ref="dataList"
          v-loading="listLoading"
          :data="menuList"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          border
          row-key="id"
          style="width:100%;margin-bottom: 20px;"
        >
          <el-table-column label="用户ID" prop="user_id"></el-table-column>
          <el-table-column label="用户编号" prop="user.user_code"></el-table-column>
          <el-table-column label="昵称" prop="user.nickname"></el-table-column>
          <!-- <el-table-column label="操作人ID" prop="deleted_admin_id"></el-table-column>
          <el-table-column label="管理员ID" prop="admin_id"></el-table-column> -->
          <el-table-column label="备注" prop="desc"></el-table-column>
          <el-table-column label="更新时间" prop="updated_at"></el-table-column>
          <el-table-column label="创建时间" prop="created_at"></el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                plain
                size="mini"
                type="primary"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>

            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :limit.sync="listQuery.perPage"
          :page.sync="listQuery.page"
          :total="total"
          @pagination="getList"
        />
      </div>
    </el-card>
    
    <el-dialog :visible.sync="userFormDialogVisible" title="输入用户ID" width="30%">
      <el-form ref="userForm" :inline="false" :model="userForm" :rules="rules" label-width="120px" size="normal">
        <el-form-item label="用户编号：" prop="user_id">
          <el-input v-model="userForm.user_id" placeholder="请输入用户编号"></el-input>
        </el-form-item>
        <el-form-item label="备注：" prop="desc">
          <el-input type="textarea" v-model="userForm.desc" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="userFormDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { listData, walletDelete, walletAdd } from '@/api/member/walletBlacklist'
export default {
  data() {
    return {
      detailDialogVisible: false,
      userInfo: {
        picture: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
        nickname: '',
        user_code: '',
        user_id: '',
        user_income: '',
        user_coin: '',
        money: ''
      },
      confirmBtn: false,
      userFormDialogVisible: false,
      listLoading: false,
      menuList: [],
      total: 10,
      listQuery: {
        page: 1,
        perPage: 10,
        user_code: undefined,
      },
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: null,
      userForm: {
        user_id: '',
        desc: ''
      },
      rules: {
        user_id: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true

      listData(this.listQuery).then(response => {
        this.menuList = response.data.data
        this.total = response.data.total
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    handleAdd() {
      this.userForm.user_id = ''
      this.userForm.desc = ''
      this.userFormDialogVisible = true
    },
    confirmAdd() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {
          walletAdd(this.userForm).then(res => {
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.getList()
              this.userFormDialogVisible = false
            }
          })

        } else {
          return false
        }
      })
    },
    // 查询
    searchSubmit() {
      this.listQuery.page = 1
      this.getList()
    },
    // 重置
    resetSubmit() {
      this.listQuery.page = 1
      this.listQuery.perPage = 10
      this.listQuery.user_code = undefined
      this.getList()
    },

    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 点击单个删除
    handleDelete(row) {
      this.$confirm('确定删除该黑名单？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
          walletDelete({
              id: row.id,
          }).then(res => {
              if(res.code === 200) {
                  this.$message({
                      type: 'success',
                      message: '操作成功'
                  })
              }
              this.getList()
          })
      }).catch(() => {
          this.$message({
              type: 'info',
              message: '已取消'
          });
      });
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        })
        return
      } else if (this.multipleSelection.length !== 0) {
        let ids = ''
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = { ids }
      }
      // this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      walletDelete(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    }
  }
}
</script>

<style scoped>
[v-cloak] {
  display: none;
}

.items {
  margin-bottom: 20px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/ .el-card__header {
  padding-bottom: 0;
}
</style>
